package IO;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;

public class Settings 
{
	public static String settings_file = SystemInfo.getApplicationDirectory() + ".settings";
	private static File getFile()
	{
		File settingFile = new File(settings_file);
		try{
			if(!verifyFile(settingFile)){
				settingFile = null;
			}
		}catch(IOException e)
		{
			System.err.println(e.getMessage());
		}
		
		return settingFile;
	}
	private static boolean writeFile(String contents)
	{
		boolean returnBool = true;
	    //use buffering
	    try {
	      Writer output = new BufferedWriter(new FileWriter(getFile()));
	      //FileWriter always assumes default encoding is OK!
	      output.write( contents );
	      output.close();
	    }
	    catch(Exception e)
	    {
	    	e.printStackTrace();
	    	returnBool = false;
	    }
	    return returnBool;
	}
	private static String readFile()
	{
		String returnString = "";
		File settingsFile = getFile();
		try
		{
			BufferedReader input =  new BufferedReader(new FileReader(settingsFile));
			try{
				String line = "";
				boolean valueFound = false;
				 while ((line = input.readLine()) != null && !valueFound)
				 {
					 returnString += line+"\n";
					 /*
					 String[] lineArray = line.split("=");
					 if(lineArray[0] == key)
					 {
						 returnValue = lineArray[1];
						 valueFound = true;
					 }
					 */
			     }
			}
			finally
			{
				input.close();
			}
		}
		catch (IOException ex)
		{
		      ex.printStackTrace();
		}
		return returnString;
	}
	private static boolean verifyFile(File aFile) throws IOException
	{
		boolean returnBool = true;
		if (!aFile.exists() && !aFile.createNewFile()) {
			returnBool = false;
		}
		if (!aFile.isFile()) {
			returnBool = false;
		}
		return returnBool;
	}
	public static String getSetting(String key)
	{
		String returnValue = null;
		String settingsFile = readFile();
		String[] settingsFileArray = settingsFile.split("\n");
		for(String settingLine : settingsFileArray)
		{			
			String[] settingLineArray = settingLine.split("=");
			if(key.equals(settingLineArray[0]))
			{
				returnValue = settingLineArray[1];
				break;
			}
		}
		return returnValue;
	}
	public static boolean setSetting(String key, String value)
	{
		String settingsFile = readFile();
		
		String pattern = key+"=.*\n";
		String replacement = "";
		if(value != null)
		{
			replacement = key+"="+value+"\n";
		}

		String newSettingsFile = "";
		boolean returnBool = true;
		if(settingsFile.contains(key))
		{
			newSettingsFile = settingsFile.replaceAll(pattern, replacement);
			returnBool = writeFile(newSettingsFile);
		}
		else if(value != null)
		{
			newSettingsFile += key+"="+value;
			returnBool = writeFile(newSettingsFile);
		}
		return returnBool;
		
	}		  
}
